package com.mlegrand.firestarter.util.write
{
	
	
	import com.mlegrand.firestarter.util.ReplaceUtil;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
		
	public class WriteContentFiles
	{
		
		protected var _location:String;
		protected var _projectName:String;
		protected var _clientName:String;
		protected var _sections:Array;
				
		
		public function WriteContentFiles(location:String, projectName:String, clientName:String, sections:Array)
		{
			_location = location
			_projectName = projectName;
			_clientName = clientName;
			_sections = sections;
		}
		
		
		public function write():void
		{
			//writeRef();
			writeMainView();
			writeModel();
			writeServiceLocator();
			
			writeController();
			writeEvent();
			writeBusiness();
			writeCommand();
		}
		
		protected function writeRef():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/rictus/reflector/Reflector.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/ref/Reflector.as"));
		}
		
		protected function writeMainView():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/view/Main.mxml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				while(t.charAt(0)!= '<')
				{
					t = t.slice(1, t.length);
				}
				t = sliceInView(t);
				t = sliceInViewNameSpaces(t);
				t = replaceDoubleDots(t);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/Main.txt"));
		}
		
		
		protected function writeContentSections(name:String, camel:String):void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				
				t = e.target.data;
				t = sliceInContentNameSpaces(t, name);
				t = t.replace('#000000', getRandomColor())
				
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+
					'/src/com/'+_clientName.toLowerCase()+
					'/'+_projectName.toLowerCase()+'/view/'+camel+'/'+name+'.mxml');
					
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				while(t.charAt(0)!= '<')
				{
					t = t.slice(1, t.length);
				}
				t = replaceSections(t, name);
				t = replaceDoubleDots(t);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/SectionView.txt"));
			writeSimpleContentForSections(name);
		}
		
		
		protected function writeSimpleContentForSections(name:String):void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+
					'/src/com/'+_clientName.toLowerCase()+
					'/'+_projectName.toLowerCase()+'/view/'+name.toLowerCase()+'/content/'+name+'Content.mxml');
					
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				while(t.charAt(0)!= '<')
				{
					t = t.slice(1, t.length);
				}
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/SectionContent.txt"));
		}

		protected function writeModel():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/model/ApplicationModel.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				var replacementString:String =  '.'+_clientName.toLowerCase()+'.'+_projectName.toLowerCase()+'.';
				t = t.replace('..', replacementString)
				t = t.slice(0,8)+ 'com.'+_clientName.toLowerCase()+'.'+_projectName.toLowerCase()+'.model' +t.slice(9,t.length);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/ApplicationModel.txt"));
		}


		protected function writeServiceLocator():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = replaceDoubleDots(t);
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/services/Services.mxml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/ServiceLocator.txt"));
		}
		
		
		
		protected function replaceDoubleDots(t:String):String
		{
			while (t.indexOf('..')!=-1)
			{
				t = t.replace('..', '.'+_clientName.toLowerCase()+'.'+_projectName.toLowerCase()+'.');
			}
			return t;
		}
		
		protected function replaceSections(t:String, name:String):String
		{
			while (t.indexOf('SECTION')!=-1)
			{
				t = t.replace('SECTION', name);
			}
			return t;
		}
		
		
		protected function sliceInView(t:String):String
		{
			var scriptPlacement:int = t.indexOf("</mx:ViewStack>")-3;
			
			//add event handlers
			for each (var sectionName:Object in _sections)
			{
				var name:String = sectionName.toString();
				var camel:String = name.charAt(0).toLowerCase() + name.slice(1,name.length);
				
				var sliceInText:String = '' + '\n'+
					'		<'+camel+':'+name+' id="'+camel+'"\n'+
					'	   					height="100%" width="100%"/>\n'  
				
				
				t = t.slice(0, scriptPlacement) + sliceInText+ t.slice(scriptPlacement, t.length);
				writeContentSections(name, camel)
				scriptPlacement = scriptPlacement+	sliceInText.length;
			}
			return t;
		}

		protected function sliceInViewNameSpaces(t:String):String
		{
			var scriptPlacement:int = t.indexOf("2006/mxml")+11;
			
			//add event handlers
			for each (var sectionName:Object in _sections)
			{
				var name:String = sectionName.toString();
				t = t.slice(0, scriptPlacement)+ 
					'' + '\n'+
					'		xmlns:'+ReplaceUtil.switchStringToCamelCase(name)+'="com..view.'+ReplaceUtil.switchStringToCamelCase(name)+'.*" '+
					t.slice(scriptPlacement, t.length);
			}
			return t;
		}

		protected function sliceInContentNameSpaces(t:String, sectionName:String):String
		{
			var scriptPlacement:int = t.indexOf("2006/mxml")+11;

			t = t.slice(0, scriptPlacement)+ 
				'' + 
				'		xmlns:content="com..view.'+ReplaceUtil.switchStringToCamelCase(sectionName)+'.content.*" \n'+
				t.slice(scriptPlacement, t.length);
			return t;
		}
		
		protected function getRandomColor():uint
		{
		    return Math.random()*0xFFFFFF;
		}
		
		
		protected function writeController():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = replaceDoubleDots(t);
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/controller/SampleLoginController.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/Control.txt"));
		}
		protected function writeBusiness():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = replaceDoubleDots(t);
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/business/SampleLoginDelegate.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/Delegate.txt"));
		}		
		protected function writeEvent():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = replaceDoubleDots(t);
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/events/SampleLoginEvent.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/Event.txt"));
		}
		protected function writeCommand():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = replaceDoubleDots(t);
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/commands/login/SampleLoginCommand.as');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/Command.txt"));
		}
		
		

	}
}